/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import model.Commodity;

/**
 *
 * @author Benjamin
 */
public class CommodityListItem extends JPanel {

 //Højde bredde konstanter:
    private static int HEIGHT = 100;
    private static int WIDTH = 287;

    //Billede konstanter: 
    private static int IMAGE_MARGIN = 3;

    private final int ATTRIBUTE_AMOUNT = 4;

    //listPanel konstanter:
    private static int LIST_PANEL_MARGIN = 3;

    //label konstaner:
    private static int LABEL_MARGIN = 6;
    private static int LABEL_HEIGHT = 19;

    private Commodity commodity;

    private Dimension size;

    private JPanel listPanel;
    private JLabel[] content;
    private JLabel image;
    private ImageIcon icon;

    public CommodityListItem(Commodity commodity) {
        this.commodity = commodity;
//        this.icon = icon;
//        panel.add(this);
        listPanel = new JPanel();
        image = new JLabel();

        this.add(listPanel);
        this.add(image);

        //Null layout for at kunne bestemme hvor labels er placeret på panelet.
        this.setLayout(null);
        listPanel.setLayout(null);

        //Standard størresle for CommodityListItem.
        Dimension size = new Dimension(WIDTH, HEIGHT);
        this.setSize(size);

        //Standard størrelse for listPanel.
        int lpX = (int) (this.getWidth() - this.getWidth() * 0.7); //70 procent af commodityListItem's samlede længde.
        int lpY = LIST_PANEL_MARGIN;
        int lpW = this.getWidth() - lpX - LIST_PANEL_MARGIN;
        int lpH = this.getHeight() - (LIST_PANEL_MARGIN * 2);
        listPanel.setBounds(lpX, lpY, lpW, lpH);

        this.setBackground(new Color(235, 235, 235));
        this.setBorder(BorderFactory.createLoweredBevelBorder());
        listPanel.setBackground(new Color(255, 255, 255));
        listPanel.setBorder(BorderFactory.createLoweredBevelBorder());

        //Tilføj labels på listPanel, og indhold til disse.
        addLabels();

        image.setBounds(IMAGE_MARGIN, IMAGE_MARGIN, this.getWidth() - listPanel.getWidth()
                - (2 * LIST_PANEL_MARGIN), this.getHeight() - (2 * LIST_PANEL_MARGIN));
        image.setBorder(BorderFactory.createLoweredBevelBorder());
        icon = new ImageIcon(commodity.getImage());
        image.setIcon(icon);

        image.revalidate();
        image.repaint();

        //Vis panel.
        listPanel.setVisible(true);
        this.setVisible(true);
        image.setVisible(true);
    }

    private void addLabels() {
        content = new JLabel[ATTRIBUTE_AMOUNT];
        for (int i = 0; i < content.length; i++) {
            content[i] = new JLabel();

            //Tilføj labels til panelet.
            listPanel.add(content[i]);

            //Placer labels.
            content[i].setBounds(LABEL_MARGIN, i * LABEL_HEIGHT + 7, listPanel.getWidth() - LABEL_MARGIN, LABEL_HEIGHT);

            //Synliggør labels.
            content[i].setVisible(true);
        }
        content[0].setText("Id: " + commodity.getId());
        content[1].setText(commodity.getName());
        content[2].setText("Antal: " + commodity.getAmount());
        content[3].setText("Minimum: " + commodity.getMinAmount());

    }

    public Commodity getCommodity() {
        return commodity;
    }

    public void setCommodity(Commodity commodity) {
        this.commodity = commodity;
    }

    public static int getHEIGHT() {
        return HEIGHT;
    }

    public static int getWIDTH() {
        return WIDTH;
    }

    public static int getIMAGE_MARGIN() {
        return IMAGE_MARGIN;
    }

    public int getATTRIBUTE_AMOUNT() {
        return ATTRIBUTE_AMOUNT;
    }

    public static int getLIST_PANEL_MARGIN() {
        return LIST_PANEL_MARGIN;
    }

    public static int getLABEL_MARGIN() {
        return LABEL_MARGIN;
    }

    public static int getLABEL_HEIGHT() {
        return LABEL_HEIGHT;
    }
}
